package com.peak.distribution.entity.goods;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;

import com.google.common.collect.Lists;
import com.peak.distribution.enums.Constants;
import com.peak.spring.boot.api.entity.LogicalDeleteEntity;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 商品
 * 
 */
@Entity
@Table(name = "dis_goods")
@Data
@EqualsAndHashCode(callSuper=true)
public class DisGoods extends LogicalDeleteEntity {

	private static final long serialVersionUID = Constants.DISTRIBUTION_VERSION;
	
	/** 货号，系统自动生成 */
	private String skuNo;
	
	/** 商品编号 */
	private String partNo;

	/** 商品名称  */
	private String name;
	
	/** 商品规格描述  */
	private String properties;
	
	/** 包装  */
	private String pack;
	
	/** 商品描述 */
	private String description;

	/** 默认图片 */
	private String defaultImage;
	
	/** 库存总数 */
	private Integer invCount;
	
	/** 最低零售价  */
	private Integer minPrice;

	/** 最高零售价  */
	private Integer maxPrice;
	
	/** 进货价格  */
	private Integer buyPrice;
	
	/** 重量：g */
	private Integer weight;
	
	/** 商品状态：false：下架；true：上架  */
	private Boolean status;
	
	/** 创建时间  */
	private Date createDate;
	
	/** 上架时间  */
	private Date onsaleDate;
	
	/** 商品组 */
	private DisGoodsGroup group;
	
	/** 价格规则 */
	private List<DisGoodsPriceRule> rules = Lists.newArrayList();

	@ManyToOne
	@JoinColumn(name = "goods_group_id")
	@Fetch(FetchMode.SELECT)
	@LazyToOne(LazyToOneOption.PROXY)
	public DisGoodsGroup getGroup() {
		return group;
	}

	@OneToMany(mappedBy = "goods")
	@Cascade(CascadeType.ALL)
	@Fetch(FetchMode.SELECT)
	@LazyCollection(LazyCollectionOption.TRUE)
	public List<DisGoodsPriceRule> getRules() {
		return rules;
	}
	
	/**
	 * 修改库存，负数就是减少
	 * @param sum
	 */
	public void addInvGoods(int sum) {
		this.invCount = this.invCount == null ? sum  : this.invCount + sum;
	}
}